package com.godGuang.patient.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.godGuang.common.core.domain.Result;
import com.godGuang.common.security.service.TokenService;
import com.godGuang.patient.feign.PayFeign;
import com.godGuang.patient.mapper.ConsumeRecordMapper;
import com.godGuang.patient.mapper.WalletMapper;
import com.godGuang.patient.service.ConsumeRecordService;
import com.godGuang.patient.service.WalletService;
import com.patient.domin.Account;
import com.patient.domin.Wallet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * @Author: XinKe-Li
 * @CreateTime: 2024-01-10  16:02
 */
@Service
public class WalletServiceImpl extends ServiceImpl<WalletMapper, Wallet> implements WalletService {
    @Qualifier("com.godGuang.patient.feign.PayFeign")
    @Autowired
    private PayFeign payFeign;

    @Autowired
    private WalletMapper walletMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private ConsumeRecordService consumeRecordService;

    @Autowired
    private ConsumeRecordMapper consumeRecordMapper;

    @Override
    public Result establishWallet(Wallet wallet) {
        Long userId = wallet.getUserId();
        LambdaQueryWrapper<Wallet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wallet::getUserId, userId);
        if (walletMapper.selectCount(queryWrapper) > 0) {
            return Result.error(0, "该用户已经创建过钱包");
        }
        int result = walletMapper.insert(wallet);
        return Result.success(result, "钱包创建成功");
    }


    @Override
    public Wallet getWalletList() {
        LambdaQueryWrapper<Wallet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wallet::getUserId, tokenService.getLoginUser().getUserid());
        return walletMapper.selectOne(queryWrapper);
    }


    @Override
    @Transactional
    public int recharge(Integer money) {
        LambdaQueryWrapper<Wallet> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Wallet::getUserId, tokenService.getLoginUser().getUserid());
        Wallet wallet = walletMapper.selectOne(queryWrapper);
        if (wallet != null) {
            Integer puresCoin = wallet.getPuresCoin();
            // 其他操作...
            LambdaUpdateWrapper<Wallet> walletLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
            walletLambdaUpdateWrapper.set(Wallet::getPuresCoin, puresCoin + (money*100));
            walletMapper.update(null,walletLambdaUpdateWrapper);
            Account account = new Account();
            account.setAccountTitle("充值");
            int i = puresCoin + (money * 100);
            account.setAccountDefault("+"+i+"币");
            account.setAccountTime(new Date());
            account.setAccountStatus(1);
            consumeRecordService.recordConsume(account);
            return 1;
        }
            // 处理未找到用户钱包的情况，可能需要返回一个标志或者抛出异常等
            return 0; // 返回0表示未找到用户钱包

    }


    @Override
    public List<Account> listAccount() {
        Long userId = tokenService.getLoginUser().getUserid();
        LambdaQueryWrapper<Account> accountQueryWrapper = new LambdaQueryWrapper<>();
        accountQueryWrapper.eq(Account::getUserId,userId);
        return consumeRecordMapper.selectList(accountQueryWrapper);
    }
}
